$(function () {
    window.pageNum=1;
    window.pageSize=5;
    window.keyword="";
    // 在全局作用域内创建Array
    window.Array = new Array();

    showpage()

});
//修改
$("#advertisementTableBody").on("click",".editBtn",function () {
    // 1.获取当前按钮的Id
    window.Id = $(this).attr("Id");
    var advertisementName=$(this).parents("tr").children("td:eq(2)").text();
    $("#advertisementNameInputEdit").val(advertisementName);
    $("#advertisementeditModal").modal("show");

})
//确认修改
$("#advertisementeditModalBtn").click(function () {
    // 1.获取文本框值
    var advertisementName = $.trim($("#advertisementNameInputEdit").val());

    if(advertisementName.length==0) {
        layer.msg("请输入有效角色名称！");

        return ;
    }

    $.ajax({
        url:"/advertisement/update/advertisement",
        type:"post",
        data:{
            "id":window.Id,
            "name":advertisementName,
            "status":advertisementStatus,
        },
        dataType:"json",
        success:function (response) {
            var result = response.result;
            if(result == "SUCCESS") {
                layer.msg("操作成功！");
                // 3.操作成功重新分页
                showpage();
            }
            if(result == "FAILED") {
                layer.msg(response.message);
            }

            // 4.不管成功还是失败，关闭模态框
            $("#advertisementaddModal").modal("hide");
        }
    });
});
//实现添加
$("#advertisementaddBtn").click(function () {
    $("#advertisementaddModal").modal("show")
});
//确认添加
$("#advertisementaddModalBtn").click(function () {
    // 1.收集文本框内容
    var advertisementName = $.trim($("#advertisementNameInput").val());
    //advertisementStatusInput
    var advertisementStatus = $.trim($("#advertisementStatusInput").val());
    if(advertisementName == null || advertisementName == "") {
        layer.msg("请输入有效角色名称！");
        return ;
    }
    if(advertisementStatus.length==0)
    {
        layer.msg("请输入有效状态！");
        return ;
    }
    $.ajax({
        url:"/advertisement/save/advertisement",
        type:"post",
        data:{
            "advertisementStatus":advertisementStatus,
            "advertisementName":advertisementName
        },
        dataType:"json",
        success:function (response) {
            var result = response.result;
            if(result == "SUCCESS") {
                layer.msg("操作成功！");
                showpage();
                $("#advertisementStatus").val("");
                $("#advertisementName").val("");
            }else {
                layer.msg(response.message);
            }
            $("#advertisementaddModal").modal("hide")
        }
    });
});

//单条删除
$("#delete").on("click", ".removeBtn", function () {
    var id = $(this).id
    window.Array.push(id)
    showRemoveConfirmModal();
})
$("#advertisementconfirmModalBtn").click(function () {
    var requestBody = JSON.stringify(window.Array);
    $.ajax({
        url: "/advertisement/batch/remove",
        type: "post",
        data: requestBody,
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        success: function (response) {
            var result = response.result;
            if (result == "SUCCESS") {
                layer.msg("操作成功！");
                // 如果删除成功，则重新调用分页方法
                showpage();
            }
            if (result == "FAILED") {
                layer.msg(response.message);
            }
            // 不管成功还是失败，都需要关掉模态框
            $("#advertisementconfirmModal").modal("hide");

        },
        "error": function (response) {
            layer.msg(response.message);
        }
    });
})
$("#searchBtn").click(function () {
    // 在单击响应函数中获取文本框中输入的数据
    var keyword = $.trim($("#keywordInput").val());

    // 验证输入数据是否有效
    if (keyword == null || keyword == "") {
        // 如果无效，提示，停止函数执行
        layer.msg("请输入关键词！");
        return;
    }

    // 如果有效，赋值给window.keyword
    window.keyword = keyword;

    // 调用showPage()重新分页
    showpage();
});

function showpage() {
    $.ajax({
        url: "/advertisement/query/for/search",
        type: "post",
        data: {
            "pageNum": window.pageNum,
            "pageSize": window.pageSize,
            "keyword": window.keyword
        },
        dataType: "JSON",
        success: function (result) {
            if (result.result == "SUCCESS") {
                //解析数据
                $("#advertisementTableBody").empty()
                //获取集合数据
                var list = result.data.list;
                //判断list有效
                if (list.length == 0) {
                    $("#advertisementTableBody")
                        .append("<tr><td colspan='4' style='text-align:center;'>没有查询到数据！</td></tr>");
                    return;
                } else {
                    for (var i = 0; i < list.length; i++) {
                        var advertisement = list[i];
                        //数据
                        var numberTd = "<td>" + (i + 1) + "</td>";
                        var advertisementNameTd = "<td>" + advertisement.name + "</td>";
                        var advertisementStatusTd = "<td>" + advertisement.status + "</td>";
                        // if(advertisementStatusTd==1){
                        //     advertisementStatusTd.toString()
                        //     advertisementStatusTd="审核中";
                        // }
                        // if(advertisementStatusTd==2){
                        //     advertisementStatusTd="审核通过";
                        // }
                        // if(advertisementStatusTd==3){
                        //     advertisementStatusTd="发布s";
                        // }
                        //按钮
                        var checkBtn = "<button type='button' class='btn btn-success btn-xs'><i class=' glyphicon glyphicon-check'></i></button>";
                        var pencilBtn = "<button Id='"+list[i].id+"' type='button' id='delete' class='btn btn-primary btn-xs editBtn'><i class=' glyphicon glyphicon-pencil'></i></button>";
                        var removeBtn = "<button Id='" + list[i].id + "' type='button' class='btn btn-danger btn-xs removeBtn'><i class=' glyphicon glyphicon-remove'></i></button>";
                        var btnTd = "<td>" + checkBtn + "" +
                            " " + pencilBtn + " " + removeBtn + "</td>";

                        var tr = "<tr>" + numberTd +  advertisementNameTd +advertisementStatusTd + btnTd + "</tr>";

                        // 将前面拼好的HTML代码追加到#advertisementTableBody中
                        $("#advertisementTableBody").append(tr);
                    }
                    //显示分页导航
                    initPagination(result.data);

                }

            } else {
                layer.msg(result.message);

            }

        }
    });
}

// 声明函数封装导航条初始化操作
function initPagination(pageIndex) {

    // 声明变量存储分页导航条显示时的属性设置
    var paginationProperties = {
        num_edge_entries: 3,        //边缘页数
        num_display_entries: 5,      //主体页数
        callback: pageselectCallback, //回调函数
        items_per_page: window.pageSize,    //每页显示数据数量，就是pageSize
        current_page: window.pageNum - 1,//当前页页码
        prev_text: "上一页",       //上一页文本
        next_text: "下一页"        //下一页文本
    };
    // 显示分页导航条
    $("#Pagination").pagination(pageIndex.total, paginationProperties);
}

// 在每一次点击“上一页”、“下一页”、“页码”时执行这个函数跳转页面
function pageselectCallback(pageIndex, jq) {
    // pageIndex从0开始，pageNum从1开始
    window.pageNum = pageIndex + 1;
    var keyword = window.pageNum;
    // 跳转页面
    showpage()
    return false
}


function showRemoveConfirmModal() {
// 1.将模态框显示出来
    $("#confirmModal").modal("show");
//显示要删除的数据
    var requestBody = JSON.stringify(window.Array);
    // 2.发送Ajax请求
    var ajaxResult = $.ajax({
        url: "/advertisement/get/list/by/id/list",
        type: "post",
        data: requestBody,
        contentType: "application/json;charset=UTF-8",
        dataType: "json",
        success: function (ajaxResult) {
            if (ajaxResult.result = "SUCCESS") {
                // 5.如果成功，显示数据
                // 3.清空#confirmModalTableBody
                $("#confirmModalTableBody").empty();
                // 4.填充#confirmModalTableBody
                var advertisementList = ajaxResult.data;
                for (var i = 0; i < advertisementList.length; i++) {
                    // 5.获取角色相关数据
                    var advertisement = advertisementList[i];
                    var id = advertisement.id;
                    var name = advertisement.name;
                    var trHTML = "<tr><td>" + id + "</td><td>" + name + "</td></tr>";

                    // 6.执行填充
                    $("#confirmModalTableBody").append(trHTML);
                }
            } else {
                layer.msg(ajaxResult.message);
                return null;
            }

        }
    })
}
